package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.WmsDeliveryDetails;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.pojo.WmsOrderDistribution;
import com.jy.wms.pojo.WmsProvinceRankingReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.jy.wms.common.base.BaseController.getJSONObject;

@Service
public class WmsDeliveryDetailsService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {

    /**
     * 发运明细
     *
     * @param
     * @return List<WmsFreightStatisticsInVariousProvinces>
     */
    public JSONObject Shippingdetails(WmsOrderDistribution mapp, String ckid, String hzid) throws ServiceException {
        JSONObject json = getJSONObject();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
       /* if(mapp.getFpsj_start()!=null){
            java.sql.Date date =new java.sql.Date(new Long(mapp.getFpsj_start()));
            mapp.setFpsj_start(sdf.format(date));
        }
        if(mapp.getFpsj_end()!=null){
            java.sql.Date date=new Date(new Long(mapp.getFpsj_end()));
            mapp.setFpsj_end(sdf.format(date));
        }*/
        try {
            PageHelper.startPage(mapp.getPage(),mapp.getRows());
            Page<WmsDeliveryDetails> list = (Page<WmsDeliveryDetails>) dao.Shippingdetails(mapp.getFpsj_start(),mapp.getFpsj_end(),ckid, hzid);

            for (WmsDeliveryDetails user : list) {
                if (!"".equals(user.getDdqdid())  && !user.getDdqdid().equals("0")) {
                    WmsOrderChannel ddqd = dao.ShippingOrderLevel(ckid, hzid, user.getDdqdid());
                    if ("0".equals(ddqd.getFjd())) {
                        user.setClassA(ddqd.getMc());
                    } else {
                        WmsOrderChannel ddqd2 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                        if ("0".equals(ddqd2.getFjd())) {
                            user.setClassA(ddqd2.getMc());
                            user.setClassB(ddqd.getMc());
                        } else {
                            WmsOrderChannel ddqd3 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                            if ("0".equals(ddqd3.getFjd())) {
                                user.setClassA(ddqd3.getMc());
                                user.setClassB(ddqd2.getMc());
                                user.setClassC(ddqd.getMc());
                            }
                        }
                    }
                }
            }
            JSONArray array = JSONArray.fromObject(list);
            json = SetStatus.setSearchType(array.toString(), (int)list.getTotal());
            return json;
        } catch (Exception e) {
            e.getMessage();
            throw new ServiceException("发运明细查询失败", e);
        }
    }

    /**
     * 发运明细导出
     *
     * @param
     * @return List<WmsFreightStatisticsInVariousProvinces>
     */
    public List<WmsDeliveryDetails> Shippingdetails(WmsDeliveryDetails WmsDeliveryDetails) throws ServiceException {
        JSONObject json = getJSONObject();
        String ckid=WmsDeliveryDetails.getCkid();
        String hzid=WmsDeliveryDetails.getHzid();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
       /* if(WmsDeliveryDetails.getFpsj_start()!=null){
            java.sql.Date date =new java.sql.Date(new Long(WmsDeliveryDetails.getFpsj_start()));
            WmsDeliveryDetails.setFpsj_start(sdf.format(date));
        }
        if(WmsDeliveryDetails.getFpsj_end()!=null){
            java.sql.Date date=new Date(new Long(WmsDeliveryDetails.getFpsj_end()));
            WmsDeliveryDetails.setFpsj_end(sdf.format(date));
        }*/
        try {
            List<com.jy.wms.pojo.WmsDeliveryDetails> list = dao.ShippingdetailsExport(WmsDeliveryDetails);

            for (com.jy.wms.pojo.WmsDeliveryDetails user : list) {
                if (!"".equals(user.getDdqdid())) {
                    WmsOrderChannel ddqd = dao.ShippingOrderLevel(ckid, hzid, user.getDdqdid());
                    if ("0".equals(ddqd.getFjd())) {
                        user.setClassA(ddqd.getMc());
                    } else {
                        WmsOrderChannel ddqd2 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                        if ("0".equals(ddqd2.getFjd())) {
                            user.setClassA(ddqd2.getMc());
                            user.setClassB(ddqd.getMc());
                        } else {
                            WmsOrderChannel ddqd3 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                            if ("0".equals(ddqd3.getFjd())) {
                                user.setClassA(ddqd3.getMc());
                                user.setClassB(ddqd2.getMc());
                                user.setClassC(ddqd.getMc());
                            }
                        }
                    }
                }
            }
            return list;
        } catch (Exception e) {
            e.getMessage();
            throw new ServiceException("发运明细查询失败", e);
        }
    }

}
